import requests
from faker import Faker
import pytest
import allure
"""
pytest+allure+reqeusts
"""
base_url = "http://8.148.229.201:8899"
faker = Faker(locale='zh_cn')
name = str(faker.name()) + "大傻蛋"
@allure.feature("用户分组流程")
class TestGroup:
    @allure.story("登录：前置")
    @pytest.fixture(name='token')
    def login(self):
        response = requests.post(url=base_url + "/adminapi/login",
                                 json={"account": "admin", "pwd": "123456", "key": "68998c008b4ff",
                                       "captchaType": "blockPuzzle", "captchaVerification": ""})
        assert response.status_code == 200
        assert response.json()['msg'] == "success"
        # 提取token，关联后续接口
        token = response.json()['data']['token']
        yield token
    @allure.story("新增用户分组")
    def test_AddGroup(self,token):
        response = requests.post(base_url + "/adminapi/user/user_group/save.html",
                                 headers={"Authori-zation": "Bearer " + str(token)}, json={"group_name": f"{name}"})
        # 打印响应体
        # print(response.json())
        assert response.status_code == 200
        assert response.json()['msg'] == "提交成功"
        assert response.json()['code'] == '100017'
    @allure.story("查询用户分组")
    def test_SearchGroup(self,token):
        # 发起查询用户分组接口请求
        response = requests.get(base_url + "/adminapi/user/user_group/list?page=1&limit=15",
                                headers={"Authori-zation": "Bearer " + str(token)})
        # print(response.json())
        assert response.status_code == 200
        assert response.json()['msg'] == "success"
        # 遍历访问查询用户分组的响应内容
        target = True
        names = []
        for i in response.json()['data']['list']:
            names.append(i['group_name'])
        if name in names:
            pass
        else:
            raise "傻蛋添加失败，没有查询到！"
# pytest [测试文件] --alluredir=allure
# allure generate ./allure/ -o ./reports --clean











